// 加载球员常规赛数据
function loadPlayerRegularData(playerId, dom){
	dom.parent().children("button.btn-info").removeClass("btn-info");
	dom.removeClass("btn-light");
	dom.addClass("btn-info");
	var data = {
		"playerId" : playerId
	}
	$("#playerData").load("/listPlayerRegularData", data);
}

//加载球员季后赛数据
function loadPlayerPostData(playerId, dom){
	dom.parent().children("button.btn-info").removeClass("btn-info");
	dom.removeClass("btn-light");
	dom.addClass("btn-info");
	var data = {
		"playerId" : playerId
	}
	$("#playerData").load("/listPlayerPostData", data);
}

// 切换到图表模式
function toRSChartPattern(playerId, type){
	var data = {
		"playerId" : playerId,
		"type" : type
	}
	$("#" + type + "DataContent").load("/toRSChartPattern", data);
	// 下面方法的第一个参数是指切换为图表的默认打开图表
	if(type == 'avg'){
		loadChart("score", playerId, "Avg", "regular", $("#avgFirstButton"));
	}else if(type == 'sum'){
		loadChart("score", playerId, "Sum", "regular", $("#sumFirstButton"));
	}else if(type == "basicAdv"){
		loadBasicAdvChart("reboundRate", playerId, "BasicAdv", "regular", $("#basicAdvFirstButton"));
	}else if(type == "shootingAdv"){
		loadShootingAdvChart("percentage", playerId, "ShootingAdv", "regular", $("#shootingAdvFirstButton"));
	}
}

// 切换到图表模式
function toPSChartPattern(playerId, type){
	var data = {
		"playerId" : playerId,
		"type" : type
	}
	$("#" + type + "DataContent").load("/toPSChartPattern", data);
	if(type == 'avg'){
		loadChart("score", playerId, "Avg", "post", $("#avgFirstButton"));
	}else if(type == 'sum'){
		loadChart("score", playerId, "Sum", "post", $("#sumFirstButton"));
	}else if(type == "basicAdv"){
		loadBasicAdvChart("reboundRate", playerId, "BasicAdv", "post", $("#basicAdvFirstButton"));
	}else if(type == "shootingAdv"){
		loadShootingAdvChart("percentage", playerId, "ShootingAdv", "post", $("#shootingAdvFirstButton"));
	}
}

// 切换到表格模式
function toRSTablePattern(playerId, type){
	var data = {
		"playerId" : playerId,
		"type" : type
	}
	$("#" + type + "DataContent").load("/toRSTablePattern" ,data)
}

// 切换到表格模式
function toPSTablePattern(playerId, type){
	var data = {
		"playerId" : playerId,
		"type" : type
	} 
	$("#" + type + "DataContent").load("/toPSTablePattern" ,data)
}

function loadBasicAdvChart(dataType, playerId, type, gameType){
	var data = {
		"dataType" : dataType,
		"playerId" : playerId,
		"gameType" : gameType
	}
	$.ajax({
		url: "/listPlayer" + type + "DataAllSeason",
		type: "POST",
		contentType : "application/json",
		data: JSON.stringify(data),
		success:function(response){
			var myChart = echarts.init($("#" + type.substring(0,1).toLowerCase() + type.substring(1) + "ChartContent").get(0));
			var gameTypeCName = gameType == "regular" ? "常规赛" : "季后赛" ;
			if(dataType == "reboundRate" || dataType == "winShare"){
				// var myChart = echarts.init($("#chartContent").get(0));
				var dataList = response.data.dataList;
				var xAxis = [];
				var data = [];
				var data2 = [];
				var data3 = [];
				var dataTypeCName = response.data.dataType;
				
				if(dataType == "reboundRate"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].reboundRate);
						data2.push(dataList[i].attackReboundRate);
						data3.push(dataList[i].defendReboundRate);
					}
				} else if(dataType == "winShare"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].winShare);
						data2.push(dataList[i].attackWinShare);
						data3.push(dataList[i].defendWinShare);
					}
				}
				var option = {
							title: {
								text: '常规赛' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: [dataTypeCName, "进攻" + dataTypeCName, "防守" + dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [
								{
									name: dataTypeCName,
									type: 'line',
									data: data
								},
								{
									name: "进攻" + dataTypeCName,
									type: 'line',
									data: data2
								},
								{
									name: "防守" + dataTypeCName,
									type: 'line',
									data: data3
								}
							]
						};
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}else if(dataType == "efficiency"){
				var dataList = response.data.efficiency;
				var xAxis = [];
				var attackEfficiency = [];
				var defendEfficiency = [];
				var dataTypeCName = response.data.dataType;
							
				for (var i=0; i<dataList.length; i++){
					xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
					attackEfficiency.push(dataList[i].attackEfficiency);
					defendEfficiency.push(dataList[i].defendEfficiency);
				}
				var option = {
							title: {
								text: '常规赛' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: ["进攻" + dataTypeCName, "防守" + dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [
								{
									name: "进攻" + dataTypeCName,
									type: 'line',
									data: attackEfficiency
								},
								{
									name: "防守" + dataTypeCName,
									type: 'line',
									data: defendEfficiency
								}
							]
						};
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true); 
			}else{
				var dataList = response.data.dataList;
				var xAxis = [];
				var data = [];
				var dataTypeCName = "";
				
				if(dataType == "assistsRate"){
					dataTypeCName = "助攻率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].assistsRate);
					}
				}else if(dataType == "stealRate"){
					dataTypeCName = "抢断率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].stealRate);
					}
				}else if(dataType == "blockShotRate"){
					dataTypeCName = "盖帽率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].blockShotRate);
					}
				}else if(dataType == "faultRate"){
					dataTypeCName = "失误率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].faultRate);
					}
				}else if(dataType == "usageRate"){
					dataTypeCName = "使用率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].usageRate);
					}
				}else if(dataType == "playerEfficiencyRating"){
					dataTypeCName = "效率值"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].playerEfficiencyRating);
					}
				}else if(dataType == "dunk"){
					dataTypeCName = "扣篮"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].dunk);
					}
				}else if(dataType == "andOne"){
					dataTypeCName = "2/3+1"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].andOne);
					}
				}
				var option = {
							title: {
								text: '常规赛' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: [dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [{
								name: dataTypeCName,
								type: 'line',
								data: data
							}]
						};
				
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}
		}
	});
}

function loadShootingAdvChart(dataType, playerId, type, gameType){
	var data = {
		"dataType" : dataType,
		"playerId" : playerId,
		"gameType" : gameType
	}
	$.ajax({
		url: "/listPlayer" + type + "DataAllSeason",
		type: "POST",
		contentType : "application/json",
		data: JSON.stringify(data),
		success:function(response){
			var myChart = echarts.init($("#" + type.substring(0,1).toLowerCase() + type.substring(1) + "ChartContent").get(0));
			
			if(dataType == "percentage" || dataType == "proportion"){
				
				var dataList = response.data.dataList;
				var xAxis = [];
				var basketData = [];
				var closeTwoData = [];
				var middleTwoData = [];
				var longTwoData = [];
				var dataTypeCName = response.data.dataType;
				
				if(dataType == "percentage"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						basketData.push(dataList[i].basketPercentage);
						closeTwoData.push(dataList[i].closeTwoPercentage);
						middleTwoData.push(dataList[i].middleTwoPercentage);
						longTwoData.push(dataList[i].longTwoPercentage);
					}
				}else if(dataType == "proportion"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						basketData.push(dataList[i].basketProportion);
						closeTwoData.push(dataList[i].closeTwoProportion);
						middleTwoData.push(dataList[i].middleTwoProportion);
						longTwoData.push(dataList[i].longTwoProportion);
					}
				}
				var option = {
							title: {
								text: '进阶数据之' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: ["篮下" + dataTypeCName, "近距离两分" + dataTypeCName, "中距离两分" + dataTypeCName, "远距离两分" + dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [
								{
									name: "篮下" + dataTypeCName,
									type: 'line',
									data: basketData
								},
								{
									name: "近距离两分" + dataTypeCName,
									type: 'line',
									data: closeTwoData
								},
								{
									name: "中距离两分" + dataTypeCName,
									type: 'line',
									data: middleTwoData
								},
								{
									name: "远距离两分" + dataTypeCName,
									type: 'line',
									data: longTwoData
								}
							]
						};
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}else if(dataType == "amount" || dataType == "attempts"){
				var dataList = response.data.dataList;
				var xAxis = [];
				var basketData = [];
				var closeTwoData = [];
				var middleTwoData = [];
				var longTwoData = [];
				var dataTypeCName = response.data.dataType;
				
				if(dataType == "amount"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						basketData.push(dataList[i].basketAmount);
						closeTwoData.push(dataList[i].closeTwoAmount);
						middleTwoData.push(dataList[i].middleTwoAmount);
						longTwoData.push(dataList[i].longTwoAmount);
					}
				}else if(dataType == "attempts"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						basketData.push(dataList[i].basketAttempts);
						closeTwoData.push(dataList[i].closeTwoAttempts);
						middleTwoData.push(dataList[i].middleTwoAttempts);
						longTwoData.push(dataList[i].longTwoAttempts);
					}
				}
				var option = {
							title: {
								text: '进阶数据之' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: ["篮下" + dataTypeCName, "近距离两分" + dataTypeCName, "中距离两分" + dataTypeCName, "远距离两分" + dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [
								{
									name: "篮下" + dataTypeCName,
									type: 'bar',
									stack: "oneSeason",
									data: basketData
								},
								{
									name: "近距离两分" + dataTypeCName,
									type: 'bar',
									stack: "oneSeason",
									data: closeTwoData
								},
								{
									name: "中距离两分" + dataTypeCName,
									type: 'bar',
									stack: "oneSeason",
									data: middleTwoData
								},
								{
									name: "远距离两分" + dataTypeCName,
									type: 'bar',
									stack: "oneSeason",
									data: longTwoData
								}
							]
						};
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}else{
				var dataList = response.data.dataList;
				var xAxis = [];
				var data = [];
				var dataTypeCName = "";
			
				if(dataType == "trueShootingPercentage"){
					dataTypeCName = "真实命中率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].trueShootingPercentage);
						
					}
				}else if(dataType == "shootingEfficiency"){
					dataTypeCName = "投篮效率"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].shootingEfficiency);
					}
				}else if(dataType == "distance"){
					dataTypeCName = "出手距离"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].distance);
					}
				}
				var option = {
							title: {
								text: '进阶数据之' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: [dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [{
								name: dataTypeCName,
								type: 'line',
								data: data
							}]
						};
				
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}// 投篮命中率 将 三分命中率 投篮命中率 两分投篮命中率 和 罚球命中率 画到一个图里
		}
	});
}

// dataType 数据类型 playerId 球员Id，type 数据表类型：场均数据、总计数据
function loadChart(dataType, playerId, type, gameType, dom){
	// dom.parent().children("button.btn-info").removeClass("btn-info");
	// dom.parent().children("button.btn").addClass("btn-default");
	// dom.removeClass("btn-default");
	// dom.addClass("btn-info");
	var data = {
		"dataType" : dataType,
		"playerId" : playerId,
		"gameType" : gameType
	}
	$.ajax({
		url: "/listPlayer" + type + "DataAllSeason",
		type: "POST",
		contentType : "application/json",
		data: JSON.stringify(data),
		success:function(response){
			var myChart = echarts.init($("#" + type.toLowerCase() + "ChartContent").get(0));
			var gameTypeCName = gameType == "regular" ? "常规赛" : "季后赛" ;
			if(dataType == "percentage" || dataType == "amount" || dataType == "attempts"){
				// var myChart = echarts.init($("#chartContent").get(0));
				var dataList = response.data.data;
				var xAxis = [];
				var shootingData = [];
				var threeData = [];
				var penaltyData = [];
				var dataTypeCName = response.data.dataType;
				
				if(dataType == "percentage"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						shootingData.push(dataList[i].shootingPercentage);
						threeData.push(dataList[i].threePointPercentage);
						penaltyData.push(dataList[i].penaltyKickPercentage);
					}
				} else if(dataType == "amount"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						shootingData.push(dataList[i].shootingAmount);
						threeData.push(dataList[i].threePointAmount);
						penaltyData.push(dataList[i].penaltyKickAmount);
					}
				}else if(dataType == "attempts"){
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						shootingData.push(dataList[i].attempts);
						threeData.push(dataList[i].threePointAttempts);
						penaltyData.push(dataList[i].penaltyKickAttempts);
					}
				}
				var option = {
							title: {
								text: gameTypeCName + '场均' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: ["投篮" + dataTypeCName, "三分" + dataTypeCName, "罚球" + dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [
								{
									name: "投篮" + dataTypeCName,
									type: 'line',
									data: shootingData
								},
								{
									name: "三分" + dataTypeCName,
									type: 'line',
									data: threeData
								},
								{
									name: "罚球" + dataTypeCName,
									type: 'line',
									data: penaltyData
								}
							]
						};
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}else if(dataType == "rebound"){
				var dataList = response.data.rebound;
				var xAxis = [];
				var rebound = [];
				var frontRebound = [];
				var backRebound = [];
				var dataTypeCName = response.data.dataType;
							
				for (var i=0; i<dataList.length; i++){
					xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
					rebound.push(dataList[i].rebound);
					frontRebound.push(dataList[i].frontRebound);
					backRebound.push(dataList[i].backRebound);
				}
				var option = {
							title: {
								text: gameTypeCName + '场均' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: [dataTypeCName, "前场" + dataTypeCName, "后场" + dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [
								{
									name: dataTypeCName,
									type: 'line',
									data: rebound
								},
								{
									name: "前场" + dataTypeCName,
									type: 'line',
									data: frontRebound
								},
								{
									name: "后场" + dataTypeCName,
									type: 'line',
									data: backRebound
								}
							]
						};
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true); 
			}else{
				var dataList = response.data.dataList;
				var xAxis = [];
				var data = [];
				var dataTypeCName = "";
			
				if(dataType == "score"){
					dataTypeCName = "得分"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].score);
					}
				}else if(dataType == "assists"){
					dataTypeCName = "助攻"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].assists);
					}
				}else if(dataType == "steal"){
					dataTypeCName = "抢断"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].steal);
					}
				}else if(dataType == "blockShot"){
					dataTypeCName = "盖帽"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].blockShot);
					}
				}else if(dataType == "fault"){
					dataTypeCName = "失误"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].fault);
					}
				}else if(dataType == "foul"){
					dataTypeCName = "犯规"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].foul);
					}
				}else if(dataType == "playingTime"){
					dataTypeCName = "上场时间"
					for (var i=0; i<dataList.length; i++){
						xAxis.push(dataList[i].season.substr(2) + "-" + String((Number(dataList[i].season) + 1)).substr(2));
						data.push(dataList[i].playingTime);
					}
				}
				var option = {
							title: {
								text: gameTypeCName + '场均' + dataTypeCName + '变化图'
							},
							legend: {
								show: true,
								data: [dataTypeCName]
							},
							tooltip: {
								trigger : "axis",
							},
							xAxis: {
								data: xAxis
							},
							yAxis: {
								type: 'value'
							},
							series: [{
								name: dataTypeCName,
								type: 'line',
								data: data
							}]
						};
				
				// 使用刚指定的配置项和数据显示图表。
				myChart.setOption(option, true);
			}// 投篮命中率 将 三分命中率 投篮命中率 两分投篮命中率 和 罚球命中率 画到一个图里
		}
	});
}
